﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using PersonalWebsite.Application.DBConnectionFactory;
using PersonalWebsite.Core.Const;
using PersonalWebsite.Core.CQRS;
using PersonalWebsite.Core.Vaildate;
using PersonalWebsite.Infrastructure;
using System;
using System.Reflection;
using Volo.Abp.Modularity;

namespace PersonalWebsite.Application;

[DependsOn(typeof(InfrastructureModule))]
public sealed class ApplicationModule : AbpModule
{
    public override void PreConfigureServices(ServiceConfigurationContext context)
    {

        context.Services.Configure<CQRSOption>(option =>
        {
            option.Assemblies = [Assembly.GetExecutingAssembly()];
           // option.BehaviorTypes = [typeof(TransactionBehavior<,>)];
        });

        context.Services.Configure<ValidateOption>(option =>
        {
            option.Assembly = Assembly.GetCallingAssembly();
        });
    }

    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        AddDbConnectionFactory(context.Services, context.Services.GetConfiguration());
    }

    /// <summary>
    /// 添加数据库连接
    /// </summary>
    /// <param name="services"></param>
    /// <param name="configuration"></param>
    /// <returns></returns>
    private IServiceCollection AddDbConnectionFactory(IServiceCollection services, IConfiguration configuration)
    {
        var connectionString = configuration.GetConnectionString(AppSettingsConst.ConnectionString.DEFAULT);

        services.AddScoped<IDBConnectionFactory, MySqlDBConnectionFactory>(c => new MySqlDBConnectionFactory(connectionString));

        return services;
    }
}
